package com.java.test.system.registrationtemplate;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.test.dto.PageResultDTO;
import com.java.test.dto.ResponseDTO;
import com.java.test.system.registrationtemplate.constant.RegistrationTemplateCodeConst;
import com.java.test.system.registrationtemplate.dto.RegistrationTemplateAddDTO;
import com.java.test.system.registrationtemplate.dto.RegistrationTemplateDTO;
import com.java.test.system.registrationtemplate.dto.RegistrationTemplateQueryDTO;
import com.java.test.system.registrationtemplate.dto.RegistrationTemplateUpdateDTO;
import com.java.test.system.registrationtemplate.entity.*;
import com.java.test.system.registrationtemplate.vo.*;
import com.java.test.timer.MessagePushTimer;
import com.java.test.util.SmartBeanUtil;
import com.java.test.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 注册模板Service
 *
 * @author lijie1
 * @date 2020/6/29 0029
 */
@Service
public class RegistrationTemplateService {

    @Autowired
    private RegistrationTemplateDAO registrationTemplateDAO;

    /**
     * 分页查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<PageResultDTO<RegistrationTemplateVO>> getSelectByList(RegistrationTemplateQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<RegistrationTemplateEntity> entityList = registrationTemplateDAO.getSelectByList(queryDTO);
        for (RegistrationTemplateEntity entity:entityList) {
            String areaName = " ";
            List<AreaEntity> areaEntityList = registrationTemplateDAO.getAreaListByIds(entity.getAreaId());
            for (AreaEntity q:areaEntityList) {
                areaName+=q.getName()+",";
            }
            entity.setAreaName(areaName.substring(0,areaName.length()-1));

            TypeEntity typeEntity = registrationTemplateDAO.getTypeListByIds(entity.getType());
            entity.setTypeName(typeEntity.getName());
            if(entity.getModifyTime()==null){
                entity.setModifyTime(entity.getCreateTime());
            }
        }
        PageResultDTO<RegistrationTemplateVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, entityList, RegistrationTemplateVO.class);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 查询地区
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<List<AreaVO>> selectByListArea() {
        List<AreaEntity> entityList = registrationTemplateDAO.selectByListArea();
        List<AreaVO> voList = SmartBeanUtil.copyList(entityList,AreaVO.class);
        return ResponseDTO.succData(voList);
    }

    /**
     * 查询字段
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<List<FieldVO>> selectByListField(RegistrationTemplateDTO dto) {
        List<TypeEntity> typeList = registrationTemplateDAO.selectToListTypeById(dto.getId());
        List<FieldEntity> entityList = new ArrayList<>();
        for (TypeEntity obj:typeList ) {
            entityList = registrationTemplateDAO.selectByListField(obj.getFieldId());
            for (int i=0;i<entityList.size();i++ ) {
                FieldEntity en = entityList.get(i);
                if("300".equals(dto.getId())){
                    if(en.getId()==1){
                        en.setStatus(false);
                    }else if (en.getId()==4){
                        entityList.remove(i);
                        entityList.add(i,entityList.get(1));
                        entityList.remove(1);
                        entityList.add(1,en);
                        en.setStatus(false);
                    }else if(en.getId()==10){
                        entityList.remove(i);
                        entityList.add(i,entityList.get(2));
                        entityList.remove(2);
                        entityList.add(2,en);
                        en.setStatus(false);
                    }
                }
                if("200".equals(dto.getId())){
                    if(en.getId()==3){
                        en.setStatus(false);
                    }else if (en.getId()==4){
                        en.setStatus(false);
                    }else if(en.getId()==10){
                        entityList.remove(i);
                        entityList.add(i,entityList.get(2));
                        entityList.remove(2);
                        entityList.add(2,en);
                        en.setStatus(false);
                    }
                }
                if("100".equals(dto.getId())){
                    if(en.getId()==2){
                        en.setStatus(false);
                    }else if (en.getId()==4){
                        en.setStatus(false);
                    }else if(en.getId()==10){
                        entityList.remove(i);
                        entityList.add(i,entityList.get(2));
                        entityList.remove(2);
                        entityList.add(2,en);
                        en.setStatus(false);
                    }
                }
            }

        }

        List<FieldVO> voList = SmartBeanUtil.copyList(entityList,FieldVO.class);
        return ResponseDTO.succData(voList);
    }

    /**
     * 查询第三方
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<List<TrilateralVO>> selectByListTrilateral() {
        List<TrilateralEntity> entityList = registrationTemplateDAO.selectByListTrilateral();
        List<TrilateralVO> voList = SmartBeanUtil.copyList(entityList,TrilateralVO.class);
        return ResponseDTO.succData(voList);
    }

    /**
     * 查询注册类型
     * @author
     * @date 2020/7/16 0016 16:05
     * @return com.java.test.dto.ResponseDTO<java.util.List < com.java.test.system.registrationtemplate.vo.TrilateralVO>>
     */
    public ResponseDTO<List<TypeVO>> selectToListType() {
        List<TypeEntity> entityList = registrationTemplateDAO.selectToListType();
        List<TypeVO> voList = SmartBeanUtil.copyList(entityList,TypeVO.class);
        return ResponseDTO.succData(voList);
    }

    /***
     * 保存
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<String> save(RegistrationTemplateAddDTO addDTO) {
        RegistrationTemplateEntity entity = registrationTemplateDAO.getByGName(addDTO.getName());
        if (entity != null) {
            return ResponseDTO.wrap(RegistrationTemplateCodeConst.ALREADY_EXIST);
        }
        registrationTemplateDAO.insertSave(addDTO);
        return ResponseDTO.succ();
    }

    /**
     * 查询数据BYId
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<RegistrationTemplateVO> selectById(String id) {
        String areaName = " ";
        String fieldName = " ";
        String trilateralName = " ";
        RegistrationTemplateEntity entity = registrationTemplateDAO.getById(id);
        if (entity == null) {
            return ResponseDTO.wrap(RegistrationTemplateCodeConst.NOT_EXIST);
        }
        List<AreaEntity> areaEntityList = registrationTemplateDAO.getAreaListByIds(entity.getAreaId());
        for (AreaEntity q:areaEntityList) {
            areaName+=q.getName()+",";
        }
        entity.setAreaName(areaName.substring(0,areaName.length()-1));
        List<FieldEntity> fieldEntityList = registrationTemplateDAO.getFieldListByIds(entity.getFieldId());
        for (FieldEntity q:fieldEntityList) {
            fieldName+=q.getName()+",";
        }
        entity.setFieldName(fieldName.substring(0,fieldName.length()-1));
        List<TrilateralEntity> trilateralEntityList = registrationTemplateDAO.getTrilateralListByIds(entity.getAreaId());
        for (TrilateralEntity q:trilateralEntityList) {
            trilateralName+=q.getName()+",";
        }
        entity.setTrilateralName(trilateralName.substring(0,trilateralName.length()-1));

        TypeEntity typeEntity = registrationTemplateDAO.getTypeListByIds(entity.getType());
        entity.setTypeName(typeEntity.getName());

        RegistrationTemplateVO configDTO = SmartBeanUtil.copy(entity, RegistrationTemplateVO.class);
        return ResponseDTO.succData(configDTO);
    }

    /**
     * 修改数据BYId
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<String> updateById(RegistrationTemplateUpdateDTO updateDTO) {
        RegistrationTemplateEntity entity = registrationTemplateDAO.getById(updateDTO.getId());
        if (entity == null) {
            return ResponseDTO.wrap(RegistrationTemplateCodeConst.NOT_EXIST);
        }
        /*RegistrationTemplateEntity alreadyEntity = registrationTemplateDAO.getByKeyExcludeId(updateDTO.getName(), updateDTO.getId());
        if (alreadyEntity != null) {
            return ResponseDTO.wrap(RegistrationTemplateCodeConst.ALREADY_EXIST);
        }*/
        registrationTemplateDAO.updateById(updateDTO);
        MessagePushTimer.registrationCLQ.offer("registrationTypePush");
        return ResponseDTO.succ();
    }

    /**
     * 删除数据BYId
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<String> deleteById(String id) {
        RegistrationTemplateEntity entity = registrationTemplateDAO.getById(id);
        if (entity == null) {
            return ResponseDTO.wrap(RegistrationTemplateCodeConst.NOT_EXIST);
        }
        registrationTemplateDAO.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * 修改状态
     * @param updateDTO
     * @author 
     * @date 2020/7/16 0016 15:06 
     * @return com.java.test.dto.ResponseDTO<java.lang.String>
     */
    public ResponseDTO<String> updateByIdToStatus(RegistrationTemplateUpdateDTO updateDTO) {
        RegistrationTemplateEntity entity = registrationTemplateDAO.getById(updateDTO.getId());
        if (entity == null) {
            return ResponseDTO.wrap(RegistrationTemplateCodeConst.NOT_EXIST);
        }
        int i = registrationTemplateDAO.updateByIdToStatus(updateDTO);
        if(i>0){
            MessagePushTimer.registrationCLQ.offer("registrationTypePush");
            return ResponseDTO.succ();
        }else {
            return ResponseDTO.error("状态更改失败！");
        }
    }

}
